package org.example.heterogeneous;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Utils {

    // int型最大值
    public static final int INF = 0x3f3f3f3f;

    /**
     * 无向图添加边
     *
     * @param edgeList
     * @param n
     * @param first
     * @return
     */
    public static List<AdjacencyEdge> addEdge(List<Edge> edgeList, int n, int[] first) {
        for (int i = 0; i < n; ++i) {
            first[i] = -1;
        }
        List<AdjacencyEdge> adjacencyEdgeList = new ArrayList<>();
        int size = 0;
        // 添加边
        // 使用邻接表存储
        for (Edge edge : edgeList) {
            int u = edge.getU();
            int v = edge.getV();
            adjacencyEdgeList.add(new AdjacencyEdge(v, first[u]));
            first[u] = size++;
            adjacencyEdgeList.add(new AdjacencyEdge(u, first[v]));
            first[v] = size++;
        }
        return adjacencyEdgeList;
    }

    /**
     * 无向图添加边，同时计算每个顶点的度
     *
     * @param edgeList
     * @param n
     * @param first
     * @param degree
     * @return
     */
    public static List<AdjacencyEdge> addEdgeWithDegree(List<Edge> edgeList, int n, int[] first, int[] degree) {
        for (int i = 0; i < n; ++i) {
            first[i] = -1;
        }
        List<AdjacencyEdge> adjacencyEdgeList = new ArrayList<>();
        int size = 0;
        // 添加边
        // 使用邻接表存储
        for (Edge edge : edgeList) {
            int u = edge.getU();
            int v = edge.getV();
            adjacencyEdgeList.add(new AdjacencyEdge(v, first[u]));
            first[u] = size++;
            ++degree[u];
            adjacencyEdgeList.add(new AdjacencyEdge(u, first[v]));
            first[v] = size++;
            ++degree[v];
        }
        return adjacencyEdgeList;
    }

    /**
     * 将core信息写入文件
     *
     * @param core
     * @param n
     * @param outputFileName
     */
    public static void writeFileCore(int[] core, int n, String outputFileName) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName, true));
            int maxCore = 0;
            for (int i = 0; i < n; ++i) {
                if (core[i] > maxCore) {
                    maxCore = core[i];
                }
                writer.write("core[" + i + "]=" + core[i] + "\n");
            }
            writer.write("maxCore=" + maxCore + "\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将k-core中的节点信息写入文件
     *
     * @param nodes
     * @param outputFileName
     */
    public static void writeFileKCore(List<Node> nodes, String outputFileName) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName, true));
            for (Node node : nodes) {
                writer.write(node + "\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到边的key，无论是边(1,2)还是边(2,1)都会得到1-2
     *
     * @param x
     * @param y
     * @return
     */
    public static String getEdgeKey(int x, int y) {
        return Math.min(x, y) + "-" + Math.max(x, y);
    }

}
